package com.yanqu.road.server.manager.guess.pb;

import com.yanqu.road.entity.guess.data.CommonGuessItem;
import com.yanqu.road.entity.guess.data.CommonGuessMain;
import com.yanqu.road.entity.guess.data.CommonGuessUserData;
import com.yanqu.road.pb.activity.CommonGuessProto;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;

public class CommonGuessPb {

    /**
     * 构造CommonGuessData
     * @param guessMain
     * @param guessItemMap
     * @param guessUserData
     * @return
     */
    public static CommonGuessProto.CommonGuessData.Builder buildCommonGuessData(CommonGuessMain guessMain, Map<String, CommonGuessItem> guessItemMap, CommonGuessUserData guessUserData) {
        CommonGuessProto.CommonGuessData.Builder dataBuilder = CommonGuessProto.CommonGuessData.newBuilder();
        dataBuilder.setType(guessMain.getType());
        dataBuilder.setPlayType(guessMain.getPlayType());
        dataBuilder.setGuessNum(guessMain.getGuessNum());
        dataBuilder.setStartTime(guessMain.getStartTime());
        dataBuilder.setEndTime(guessMain.getEndTime());
        dataBuilder.setEndWaitResultTime(guessMain.getEndWaitResultTime());
        dataBuilder.setEndShowTime(guessMain.getEndShowTime());
        if (guessUserData != null && !StringUtils.isNullOrEmpty(guessUserData.getObjIds())) {
            if (!StringUtils.isNullOrEmpty(guessUserData.getObjIds())) {
                List<String> myObjIds = StringUtils.stringToStringList(guessUserData.getObjIds(), ",");
                dataBuilder.addAllMyObjIds(myObjIds);
            }
            dataBuilder.setGetRewardFlag(guessUserData.getGetRewardFlag());
        }
        for (CommonGuessItem guessItem : guessItemMap.values()) {
            CommonGuessProto.CommonGuessItem.Builder itemBuilder = CommonGuessProto.CommonGuessItem.newBuilder();
            itemBuilder.setObjId(guessItem.getObjId());
            itemBuilder.setGuessUserNum(guessItem.getGuessUserNum());
            dataBuilder.addGuessItem(itemBuilder);
        }
        if (!StringUtils.isNullOrEmpty(guessMain.getRankResult())) {
            List<String> rankResult = StringUtils.stringToStringList(guessMain.getRankResult(), ",");
            dataBuilder.addAllResultObjIds(rankResult);
        }
        return dataBuilder;
    }
}
